#include <iostream>

using namespace std;


template <class T>
struct Node
{ 
    T data;
    Node* next;
};

template <class T>
class Chain
{
public:
    Chain(int n):capacity(n)
    {
        listbegin=(Node<T>*)new Node<T>;
        listbegin->next=NULL;
        listbegin->data=0;
        head=listbegin;
        for(int i=0;i<capacity;i++)
        {
            Node<T>* temp=(Node<T>*)new Node<T>;
            temp->next=NULL;
            temp->data=0;
            head->next=temp;
            head=temp;
        }
    };

    // FIXME: 析构没有实现，最好把内存释放了
    ~Chain(){};
    void insert(int index,T element);
    void earse(int index);
    void display();
    int get();
    int size();
private:
    Node<T>* listbegin;
    Node<T>* head;
    int capacity;
};


template<class T>
int Chain<T>::size()
{
    return capacity;
};

template<class T>
void Chain<T>::insert(int index,T element)
{
    head=listbegin;
    for(int i=0;i<index;i++)
    {
        listbegin=listbegin->next;
    }
    Node<T>* temp=(Node<T>*)new Node<T>;
    temp->data=element;
    temp->next=listbegin->next;
    listbegin->next=temp;
    capacity++;
};

template<class T>
void Chain<T>::earse(int index)
{
    head=listbegin;
    for(int i=1;i<index;i++)
    {
        listbegin=listbegin->next;
    }
    Node<T>* temp=(Node<T>*)new Node<T>;
    temp=listbegin->next;
    listbegin->next=listbegin->next->next;
    free(temp);
    capacity--;
};

template<class T>
void Chain<T>::display()
{
    Node<T>* head=listbegin;
    while(head!=NULL)
    {
        cout<<head->data<<" ";
        head=head->next;
    }
};

int main()
{
    Chain<int> a(10);
    a.display();
    a.insert(3,3);
    cout<<a.size()<<endl;
    a.display();
    a.earse(2);
    cout<<a.size()<<endl;
    a.display();

    system("pause");
    return 0;
}




